package cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.controller.admin.banner.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.convert.banner.BannerConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.dal.dataobject.banner.BannerDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.dal.mysql.banner.BannerMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.banner.service.banner.BannerService;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.dataobject.turntable.TurntableDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.turnTable.dal.mysql.turntable.TurntableMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 小程序-banner")
@RestController
@RequestMapping("/qd/banner")
@Validated
public class BannerController {

    @Resource
    private BannerService bannerService;
    @Resource
    BannerMapper bannerMapper;
    @Resource
    RedisTemplate<String, Object> redis;
    @Resource
    TurntableMapper turntableMapper;

    //获取轮盘list
    @GetMapping("getTurnList")
    public CommonResult getTurnList(){
        List<Map<String,Object>> list=new ArrayList<>();
        for (TurntableDO vo : turntableMapper.selectList(new LambdaQueryWrapperX<TurntableDO>().eq(TurntableDO::getStatus,1))) {
            Map<String,Object> map=new HashMap<>();
            map.put("id",vo.getId());
            map.put("name",vo.getName());
            list.add(map);
        }

        return success(list);
    }

    //获取信息
    @GetMapping("getInfo")
    public CommonResult getInfo(){
        String content="";
        Object o = redis.opsForValue().get("content");
        if (o!=null)
            content=o.toString();
        return success(content);
    }

    //修改显示状态
    @GetMapping("updateInfo")
    public CommonResult updateInfo(String content){
        redis.opsForValue().set("content",content);
        return success("");
    }


    //修改显示状态
    @GetMapping("updateStatus")
    public CommonResult updateStatus(Integer id){
        BannerDO bannerDO = bannerMapper.selectById(id);
        Integer status = bannerDO.getStatus();
        if (status==1){
            status=2;
        }else {
            status=1;
        }
        bannerDO.setStatus(status);
        bannerMapper.updateById(bannerDO);
        return success("");
    }

    @PostMapping("/create")
    @Operation(summary = "创建小程序-banner")
    @PreAuthorize("@ss.hasPermission('qd:banner:create')")
    public CommonResult<Integer> createBanner( @RequestBody BannerDO createReqVO) {
        return success(bannerService.createBanner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序-banner")
    @PreAuthorize("@ss.hasPermission('qd:banner:update')")
    public CommonResult<Boolean> updateBanner( @RequestBody BannerDO updateReqVO) {
        bannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序-banner")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:banner:delete')")
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") Integer id) {
        bannerService.deleteBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序-banner")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:banner:query')")
    public CommonResult getBanner(@RequestParam("id") Integer id) {
        return success(bannerService.getBanner(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得小程序-banner列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:banner:query')")
    public CommonResult<List<BannerRespVO>> getBannerList(@RequestParam("ids") Collection<Integer> ids) {
        List<BannerDO> list = bannerService.getBannerList(ids);
        return success(BannerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序-banner分页")
    @PreAuthorize("@ss.hasPermission('qd:banner:query')")
    public CommonResult<PageResult<BannerRespVO>> getBannerPage(@Valid BannerPageReqVO pageVO) {
        PageResult<BannerDO> pageResult = bannerService.getBannerPage(pageVO);
        return success(BannerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序-banner Excel")
    @PreAuthorize("@ss.hasPermission('qd:banner:export')")
    @OperateLog(type = EXPORT)
    public void exportBannerExcel(@Valid BannerExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BannerDO> list = bannerService.getBannerList(exportReqVO);
        // 导出 Excel
        List<BannerExcelVO> datas = BannerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "小程序-banner.xls", "数据", BannerExcelVO.class, datas);
    }

}
